package com.shanghaionstar.home.function;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.function.FunctionInfo;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.home.function.controller.DragController;
import com.shanghaionstar.home.function.layout.DragLayer;
import com.shanghaionstar.home.function.layout.FunctionListLayout;
import com.shanghaionstar.home.function.layout.Hotseat;
import com.shanghaionstar.home.iview.IEditShortCutView;
import com.shanghaionstar.home.presenter.EditShortCutPresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.List;

/**
 * Created by kai zhang on 2016/1/21.
 */
@ContentView(R.layout.onstar_fragment_function_selection)
public class EditShortCutActivity extends SwipeActivity
        implements IEditShortCutView, View.OnLongClickListener, View.OnClickListener {
    @FindViewById(R.id.function_selection_root)
    private DragLayer mDragLayer;
    @FindViewById(R.id.function_selection_list_layout)
    private FunctionListLayout mEditList;
    @FindViewById(R.id.function_selection_hotseat)
    private Hotseat mHotSeat;
    @FindViewById(R.id.function_selection_sort_button)
    private Button mSortBtn;
    @FindViewById(R.id.function_selection_close_button)
    private ImageButton mCloseIb;

    private DragController mDragController;
    private int hotSeatIconNumber;
    private EditShortCutPresenter presenter;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        presenter = new EditShortCutPresenter(getScheduler());
        presenter.setView(this);
        presenter.getShortCutData();
    }

    @Override
    public BasePresenter getPresenter() {
        return presenter;
    }


    // F5546 EditAndDelete REPORT 快捷编辑-排序删除按钮

    @Override
    public boolean onLongClick(View v) {
        if (!mDragController.isDragging()) {
            if (v instanceof BubbleTextView) {
                if (((BubbleTextView) v).getBelongTo() == BubbleTextView.BelongTo.Hotseat) {
                    mDragController.startDrag(v, mHotSeat, v.getTag());
                } else if (((BubbleTextView) v).getBelongTo() == BubbleTextView.BelongTo.FunctionList) {
                    mDragController.startDrag(v, mEditList, v.getTag());
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (!mDragController.isDragging() && v instanceof BubbleTextView) {
            if (!((BubbleTextView) v).hasPerformedLongPress()) {
                if (((BubbleTextView) v).getBelongTo() == BubbleTextView.BelongTo.FunctionList) {
                    if (mHotSeat.getHotseatFunctionInfoDatas().size() < 8) {
                        mDragController.quickMove(v, mEditList, mHotSeat,
                                v.getTag());
                    } else {
                        new ToastView(EditShortCutActivity.this).showToast(getString(R.string.shortcut_cannot_add_more));
                    }
                }
            }
        } else if (v.getId() == R.id.function_selection_sort_button) {
            BusinessUtil.sendReportByClickType(EditShortCutActivity.this,
                    FunctionNameFindId.EditAndDelete.getStringValue());
            if (getString(R.string.function_sort_delete).equals(mSortBtn.getText())) {
                hotSeatIconNumber = mHotSeat.getHotseatFunctionInfoDatas().size();
                mSortBtn.setText(getString(R.string.function_sort_complete));
                mEditList.setVisibility(View.INVISIBLE);
                mHotSeat.setOnLongClickListener(this);
                mHotSeat.showDeleteMode();
            } else {
                mSortBtn.setText(getString(R.string.function_sort_delete));
                int newNumber = mHotSeat.getHotseatFunctionInfoDatas().size();
                if (hotSeatIconNumber != newNumber) {
                    presenter.reFreshEditList(mHotSeat.getHotseatFunctionInfoDatas());
                }
                mHotSeat.setOnLongClickListener(null);
                mEditList.setVisibility(View.VISIBLE);
                mHotSeat.dismissDeleteMode();
            }
        } else if (v.getId() == R.id.function_selection_close_button) {
            if (!getString(R.string.function_sort_delete).equals(mSortBtn.getText())) {
                showTipsDia();
            } else {
                presenter.saveData(mHotSeat.getHotseatFunctionInfoDatas());
                finish();
//                overridePendingTransition(R.anim.scale_out, R.anim.slide_out_to_bottom);
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        presenter.saveData(mHotSeat.getHotseatFunctionInfoDatas());
    }


    private void showTipsDia() {
        AlertDialog.Builder builder = new AlertDialog.Builder(EditShortCutActivity.this)
                .setMessage(R.string.shortcut_msg)
                .setPositiveButton(getString(R.string.shortcut_discard_changes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton(getString(R.string.shortcut_save), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mSortBtn.performClick();
                    }
                });
        AlertDialog dia = builder.create();
        Window window = dia.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount = 0.5f;
        window.setAttributes(params);
        dia.show();
    }

    @Override
    public void initView(List<FunctionInfo> editList, List<FunctionInfo> hotSeatList) {
        mEditList.setOnClickListener(this);
        mHotSeat.setOnClickListener(this);
        mHotSeat.setDatas(hotSeatList);
        mEditList.setDatas(editList);
        mDragController = new DragController(mDragLayer);
        mDragLayer.setDragController(mDragController);
        mDragController.addDropTarget(mEditList);
        mDragController.addDragListener(mEditList);
        mDragController.addDropTarget(mHotSeat);
        mDragController.addDragListener(mHotSeat);
        mSortBtn.setOnClickListener(this);
        mCloseIb.setOnClickListener(this);
    }

    @Override
    public void reFreshEditList(List<FunctionInfo> mDataList) {
        mEditList.setDatas(mDataList);
    }

    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestory();
        presenter = null;
    }

    @Override
    public Activity onActivity() {
        return EditShortCutActivity.this;
    }
}
